package day14.Executors;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Lian02 {
    public static void main(String[] args) {
//        method1();
//        method2();
//        method3();
//  核心线程,
//  最大线程,
//  空闲线程最大存活时间,
//  时间单位,
//  任务队列,
//  创建线程工场,
//  任务的拒绝策略
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                1,
                2,
                2,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
        for (int i = 1; i <= 10; i++) {
            int a=i;
            pool.submit(()->System.out.println(Thread.currentThread().getName()+"---"+a));
        }
        pool.shutdown();
    }

    private static void method3() {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                1,
                2,
                2,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());
        for (int i = 1; i <= 10; i++) {
            int a=i;
            pool.submit(()->System.out.println(Thread.currentThread().getName()+"---"+a));
        }
        pool.shutdown();
    }

    private static void method2() {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                1,
                2,
                2,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());
        for (int i = 1; i <= 5; i++) {
            int a=i;
            pool.submit(()->System.out.println(Thread.currentThread().getName()+"---"+a));
        }
        pool.shutdown();
    }

    private static void method1() {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2,
                5,
                2,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        for (int i=1;i<=15;i++) {
            pool.submit(new MyRunnable());
        }

        pool.shutdown();
    }
}
class MyRunnable implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"在执行了");
    }
}
